<?php

namespace app\index\controller;

use think\Controller;
use app\index\model\SponsorModel;
use think\Request;
use app\index\controller\Basecontroller;

class Sponsor extends Basecontroller
{
  protected $sponsor = null;
  // public $path ='http://localhost/tp5/public/uploads/';

  function __construct()
  {
    parent::__construct();
    $this->sponsor = new SponsorModel();
  }

  function getSort()
  {
    $result = $this->sponsor->getSort();

    return res(1000, 'ok', $result);
  }

  public function upload_pic()
  {
    // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file('file');

    // 移动到框架应用根目录/public/uploads/ 目录下
    if ($file) {
      $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
      if ($info) {
        $result = str_replace('\\', "/", $info->getSaveName());;

        return res(1000, '上传成功', $result);
      } else {
        // 上传失败获取错误信息
        echo $file->getError();
      }
    }
  }

  public function submit_total()
  {

    $detail_pic_url_arr = [];
    //获取缩略图路径
    $thumbnail_file = request()->file('thumbnail_file');
    $thumbnail_info = $thumbnail_file->move(ROOT_PATH . 'public' . DS .  'uploads');
    $thumbnail_url = str_replace('\\', "/", $thumbnail_info->getSaveName());

    //获取长图路径
    $long_pic_file = request()->file('long_pic_file');
    $long_pic_info = $long_pic_file->move(ROOT_PATH . 'public' . DS .  'uploads');
    $long_pic_url = str_replace('\\', "/", $long_pic_info->getSaveName());

    //获取详情图路径
    $detail_pic_num = (int)$_POST['detail_pic_num'];


    for ($i = 0; $i < $detail_pic_num; $i++) {
      $detail_pic_file = request()->file('detail_pic_file' . $i);
      $detail_pic_info = $detail_pic_file->move(ROOT_PATH . 'public' . DS .  'uploads');

      $detail_pic_url = str_replace('\\', "/", $detail_pic_info->getSaveName());

      array_push($detail_pic_url_arr, $detail_pic_url);

    }



    //获取项目视频路径
    $video_file = request()->file('video_file');
    $video_file_info = $video_file->move(ROOT_PATH . 'public' . DS .  'uploads');
    $project_video_url = str_replace('\\', "/", $video_file_info->getSaveName());

    //获取档位信息
    $level_data_arr = json_decode($_POST['level_data_arr'], true);


    //获取档位图片路径
    $level_pic_num = (int)$_POST['level_pic_num'];
    $level_pic_url_arr = [];
    for ($i = 0; $i < $level_pic_num; $i++) {
      $level_pic_file = request()->file('level_pic_file' . $i);
      $level_pic_info = $level_pic_file->move(ROOT_PATH . 'public' . DS .  'uploads');
      $level_pic_url = str_replace('\\', "/", $level_pic_info->getSaveName());
      array_push($level_pic_url_arr, $level_pic_url);
    }


    $token = $_POST['AccessToken'];
    $tokenData = json_decode($this->redis->get($token), true);
    $uid = $tokenData['uid'];


    $basic_data = [
      'keep_time' => $_POST['days'] * 24 * 60 * 60,
      'g_name' => $_POST['project_name'],
      'g_longImg' => $this->file_path . $long_pic_url,
      'g_picture' => $this->file_path . $thumbnail_url,
      'g_video' => $this->file_path . $project_video_url,
      'g_sponsor' => $uid,
      'g_start_money' => 0,
      'g_text' => $_POST['desc'],
      'followers_num' => 0,
      'likes_num' => 0,
      'buy_num' => 0,
      'g_state' => 1,
      'g_end_money' => $_POST['money'],
      'g_create_time' => date('Y-m-d h:i:sa'),
      'cid' => $_POST['sort'],
      'start_time' => 0,
    ];


    $gid = $this->sponsor->addBasicData($basic_data);

    $result = 0;

    for ($i = 0; $i < count($detail_pic_url_arr); ++$i) {
      $detail_pic_url = $detail_pic_url_arr[$i];
      $detail_pic_ur_date = [
        'gid' => $gid,
        'imgsrc' => $this->file_path . $detail_pic_url,
      ];
      $result = $this->sponsor->addDetailPic($detail_pic_ur_date);
    }


    for ($i = 0; $i < $level_pic_num; ++$i) {
      $level_name = $level_data_arr[$i]['level_name'];
      $level_money = $level_data_arr[$i]['level_money'];
      $level_quota = $level_data_arr[$i]['level_quota'];
      $level_return_content = $level_data_arr[$i]['level_return_content'];
      $level_return_time = $level_data_arr[$i]['level_return_time'];
      $level_pic_url = $level_pic_url_arr[$i];


      $level_date_arr = [
        'gid' => $gid,
        'name' => $level_name,
        'pic' => $this->file_path . $level_pic_url,
        'desc' => $level_return_content,
        'price' => $level_money,
        'buyer' => 0,
        'limit_num' => $level_quota,
        'store_num' => $level_quota,
        'return_time' => $level_return_time * 24 * 60 * 60,
      ];
      $result = $this->sponsor->addLevelDate($level_date_arr);
    }

    if ($result == 1) {
      return res(1000, '添加成功');
    } else {
      return res(1001, '添加失败');
    }
  }
}
